/*******************************************************************************
 * Copyright (c) 2011 Irineu Martins de Lima Moura (imlm2).
 * This file is part of JBeat.
 * 
 *     JBeat is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     JBeat is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with JBeat.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Contributors:
 *     Irineu Martins de Lima Moura (imlm2) - initial API and implementation
 ******************************************************************************/
package br.ufpe.cin.imlm2.jbeat.soot.throwableset.tags;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import soot.SootClass;
import soot.tagkit.AttributeValueException;
import soot.tagkit.Tag;

/**
 * A tag that contains all the possible exceptions that a given method may throw.
 * @author Irineu
 *
 */
public class ThrowableSetTag implements Tag {
	
	public static final String TAG_NAME = "ThrowableSetTag";
	
	private Set<SootClass> exceptions;
	
	public ThrowableSetTag() {
		this.exceptions = new HashSet<SootClass>();
	}
	
	public void addAllExceptions(Set<SootClass> exceptions){
		this.exceptions.addAll(exceptions);
	}
	
	public void addException(SootClass e){
		this.exceptions.add(e);
	}
	
	public void removeException(SootClass e){
		this.exceptions.remove(e);
	}
	
	public Set<SootClass> getThrowableSet(){
		return this.exceptions;
	}

	@Override
	public String getName() {
		return TAG_NAME;
	}

	@Override
	public byte[] getValue() throws AttributeValueException {
		return null;
	}

	@Override
	public String toString() {
		Iterator<SootClass> excepts = this.exceptions.iterator();
		StringBuffer sb = new StringBuffer();
		while(excepts.hasNext()){
			SootClass next = excepts.next();
			sb.append(next.getName()+",");
		}
		if(sb.length() > 0)	sb.deleteCharAt(sb.length() - 1);
		return sb.toString();
	}
	
	

}
